//
//  PresentedPartTransition.swift
//  CosClub
//
//  Created by 沙睿 on 2020/4/1.
//  Copyright © 2020 ios. All rights reserved.
//

import Foundation
import UIKit

public class PresentedPartTransition: NSObject, UIViewControllerAnimatedTransitioning
{
    public enum TransitionType {
        case present
        case dismiss
    }
    
    public var type :TransitionType = .present
    public var vcHeight: CGFloat = 0
    
    public convenience init(type: TransitionType, height: CGFloat) {
        self.init()
        self.type = type
        self.vcHeight = height
    }
    
    public override init() {
        super.init()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    public func transitionDuration(using transitionContext: UIViewControllerContextTransitioning?) -> TimeInterval {
        0.3
    }
    
    public func animateTransition(using transitionContext: UIViewControllerContextTransitioning) {
        
        if self.type == .present {
            //通过viewControllerForKey取出转场前后的两个控制器，这里toVC就是vc1、fromVC就是vc2
            let toVC = transitionContext.viewController(forKey: .to)
            let fromVC = transitionContext.viewController(forKey: .from)
            
            // 捕捉当前镜像
            //        let tempView = fromVC?.view.snapshotView(afterScreenUpdates: false)
            //        tempView?.frame = fromVC?.view.frame ?? CGRect.zero
            
            //        fromVC?.view.isHidden = true
            // 过度动画容器
            let containerView = transitionContext.containerView
            containerView.backgroundColor = "000000".color(alpha: 0.3)
            
            //        containerView.addSubview(tempView!)
            containerView.addSubview(toVC!.view!)
            
            toVC?.view.frame = CGRect(x: 0, y: containerView.height, width: containerView.width, height: self.vcHeight)
            
            
            UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
                toVC?.view.transform = CGAffineTransform(translationX: 0, y: -self.vcHeight);
                //            tempView?.transform = CGAffineTransform(scaleX: 0.85, y: 0.85);
            }) { (finish) in
                transitionContext.completeTransition(!transitionContext.transitionWasCancelled)
                if transitionContext.transitionWasCancelled {
                    //                fromVC?.view.isHidden = false
                    //                tempView?.removeFromSuperview()
                }
            }
        }else {
            
            let fromVC = transitionContext.viewController(forKey: .from)
            //参照present动画的逻辑，present成功后，containerView的最后一个子视图就是截图视图，我们将其取出准备动画
            let containerView = transitionContext.containerView
            let subviewsArray = containerView.subviews;
            
            let tempView = subviewsArray.last!
            //            //动画吧
            
            UIView.animate(withDuration: self.transitionDuration(using: transitionContext), animations: {
                //因为present的时候都是使用的transform，这里的动画只需要将transform恢复就可以了
                fromVC?.view.transform = .identity
                tempView.transform = .identity
            }) { (finish) in
                if transitionContext.transitionWasCancelled {
                    //标记失败
                    transitionContext.completeTransition(false)
                }else {
                    transitionContext.completeTransition(true)
                    tempView.removeFromSuperview()
                }
            }
        }
    }
}
